package com.cheer.dao;

import com.cheer.entity.ExMatchAwardEntity;
import com.cheer.enums.LotteryActTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author cheer
 */
@Repository
@Slf4j
public class PrizeCacheDao {

    /**
     * 答题使用key
     */
    private static final String PRIZE_KEY = "net:match:prize";

    /**
     * H5使用key
     */
    private static final String H5_PRIZE_KEY = "h5:match:prize";

    /**
     * 网络安全使用key
     */
    private static final String SAFE_PRIZE_KEY = "safe:match:prize";

    @Autowired
    private ExMatchAwardDao exMatchAwardDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public Boolean addPrize(Long awardId) {
        try {
            ExMatchAwardEntity exMatchAwardEntity = exMatchAwardDao.selectById(awardId);
            // 积分则放在积分抽奖中
            if(LotteryActTypeEnum.QUESTION.getCode().equals(exMatchAwardEntity.getAtc())){
                stringRedisTemplate.opsForList().leftPush(PRIZE_KEY, String.valueOf(awardId));
            }else if (LotteryActTypeEnum.INTEGRAL.getCode().equals(exMatchAwardEntity.getAtc())){
                stringRedisTemplate.opsForList().leftPush(H5_PRIZE_KEY, String.valueOf(awardId));
            }else if (LotteryActTypeEnum.SECURITY.getCode().equals(exMatchAwardEntity.getAtc())){
                stringRedisTemplate.opsForList().leftPush(SAFE_PRIZE_KEY, String.valueOf(awardId));
            }
            return true;
        } catch (Exception e) {
            log.error("add prize fail", e);
            return false;
        }
    }

    public String getPrize() {
        // 单独切 一个网络安全的版本出来
        // return stringRedisTemplate.opsForList().rightPop(SAFE_PRIZE_KEY);
        return stringRedisTemplate.opsForList().rightPop(PRIZE_KEY);
    }
}
